package com.hmyuan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmyuan.dao.ArticleMapper;
import com.hmyuan.dao.Article_categoryMapper;
import com.hmyuan.dao.Article_tagMapper;
import com.hmyuan.domain.Article;
import com.hmyuan.domain.Article;
import com.hmyuan.domain.Article_category;
import com.hmyuan.domain.Article_tag;
import com.hmyuan.service.ArticleService;
import com.hmyuan.service.ArticleService;
import com.hmyuan.util.PageUtil;
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
	@Resource
	ArticleMapper articleMapper;
	@Resource
	Article_categoryMapper article_categoryMapper;
	@Resource
	Article_tagMapper article_tagMapper;
	@Resource
	PageUtil pageUtil;
	
	@Override
	public PageInfo<Article> selectListByPage(Article article,
			int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); // 核心分页代码
		List<Article> list = articleMapper.selectList(article);
		PageInfo<Article> pageInfo = new PageInfo<Article>(list);
		
		return pageInfo;
	}

	@Override
	public boolean insert(Article article) {
		// TODO Auto-generated method stub
		article.setContent(article.getContent().replaceAll((String)pageUtil.getValueOfAplication("fileDomainName"), "~~fileDomainName~~"));
		article.setTopimg(article.getTopimg().replaceAll((String)pageUtil.getValueOfAplication("fileDomainName"), "~~fileDomainName~~"));
		int num = articleMapper.insert(article);
		return num==1?true:false;
	}

	@Override
	public Article selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		Article article = articleMapper.selectByPrimaryKey(id);
		article.setContent(article.getContent().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
		article.setTopimg(article.getTopimg().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
		return article;
	}

	@Override
	public boolean updateByPrimaryKeySelective(Article article) {
		// TODO Auto-generated method stub
		
		article.setContent(article.getContent()==null?null:article.getContent().replaceAll((String)pageUtil.getValueOfAplication("fileDomainName"), "~~fileDomainName~~"));
		article.setTopimg(article.getTopimg()==null?null:article.getTopimg().replaceAll((String)pageUtil.getValueOfAplication("fileDomainName"), "~~fileDomainName~~"));
		int num = articleMapper.updateByPrimaryKeySelective(article);
		return num==1?true:false;
	}

	@Override
	public boolean deletearticleById(Long id) {
		// TODO Auto-generated method stub
		/*删除关联信息*/
		delete_article_categoryByArticleid(id);
		delete_article_tagsByArticleid(id);
		int num = articleMapper.deleteByPrimaryKey(id);
		return num==1?true:false;
	}

	@Override
	public boolean insert_article_category(Article_category article_category) {
		// TODO Auto-generated method stub
		int num = article_categoryMapper.insert(article_category);
		return num==1?true:false;
	}

	@Override
	public boolean insert_article_tags(Long articleid, Long[] tags) {
		// TODO Auto-generated method stub
		for (long tagid : tags) {
			Article_tag article_tag = new Article_tag();
			article_tag.setArticleid(articleid);
			article_tag.setTagid(tagid);
			article_tagMapper.insert(article_tag);
		}
		return true;
	}

	@Override
	public boolean delete_article_categoryByArticleid(Long articleid) {
		// TODO Auto-generated method stub
		article_categoryMapper.delete_article_categoryByArticleid(articleid);
		return true;
	}

	@Override
	public boolean delete_article_tagsByArticleid(Long articleid) {
		// TODO Auto-generated method stub
		article_tagMapper.delete_article_tagsByArticleid(articleid);
		return true;
	}

	@Override
	public PageInfo<Article> selectListWithCollectionByPage(Article article,
			int pageNum, int pageSize) {
		PageInfo<Article> pageInfo = new PageInfo<Article>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		article.setPageBigen((pageNum-1)*pageSize);
		article.setPageSize(pageSize);
		List<Article> list = articleMapper.selectListWithCollection(article);
		pageInfo.setList(list);
		Long count = articleMapper.selectListWithCollectionCout(article);
		pageInfo.setTotal(count);
		pageInfo.setPages((int) ((count+pageSize-1)/pageSize));
		return pageInfo;
	}

	@Override
	public Article selectByWithCollectionPrimaryKey(Long id) {
		if(id==null) return null;
		Article article = articleMapper.selectByWithCollectionPrimaryKey(id);
		article.setContent(article.getContent().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
		article.setTopimg(article.getTopimg().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
		return article;
	}

	@Override
	public List<Article> selectList(Article article) {
		// TODO Auto-generated method stub
		return articleMapper.selectList(article);
	}

	@Override
	public Article selectByPassword(String password) {
		// TODO Auto-generated method stub
		List<Article> list = articleMapper.selectByPassword(password);
		if(list.size()>0){
			Article article = list.get(0);
			article.setContent(article.getContent().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
			article.setTopimg(article.getTopimg().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
			return article;
		}else{
			return null;
		}
	}
}
